<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>高阶函数</title>
  </head>
  <body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!-- 引入react核心库 -->
    <script src="../js/react.development.js"></script>

    <!-- 引入react-dom用于支持react操作DOM  -->
    <script src="../js/react-dom.development.js"></script>

    <!-- 引用bable,用于将jsx转为js -->
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
      // 创建组件
      class Login extends React.Component {
        // 初始化状态
        state = {
          username: '',
          password: '',
        }

        saveFormData = (dataType) => {
          return (event) => {
            this.setState({ [dataType]: event.target.value })
          }
        }

        // 表单提交的回调
        handleSubmit = (event) => {
          event.preventDefault()
          const { username, password } = this.state
          alert(`你输入的用户名是： ${username}, 你输入的密码是： ${password}`)
        }

        render() {
          return (
            <form action="http://atguigu.com" onSubmit={this.handleSubmit}>
              用户名：{' '}
              <input
                onChange={this.saveFormData('username')}
                type="text"
                name="username"
              />
              密码：{' '}
              <input
                onChange={this.saveFormData('password')}
                type="password"
                name="password"
              />
              <button>登录</button>
            </form>
          )
        }
      }
      // 渲染登录界面到页面
      ReactDOM.render(<Login />, document.getElementById('test'))
    </script>
  </body>
</html>
